/**
    @author: ASUS
    @date: 2021/2/21
    @Describe:  读取器
**/
package kernel

import (
	"bufio"
	"fmt"
	"io"
	"logprocess/global/monitor"
	"os"
)

const TypeHandleLine = iota //记录读取的行数

type Reader interface {
	Reads(rc chan []byte)
}

type ReadFromFile struct {
	Path string //文件的路径
}

func (r *ReadFromFile) Reads(rc chan []byte) {
	file, err := os.Open(r.Path)
	if err != nil {
		panic(fmt.Sprintf("open file error:%s", err.Error()))
	}
	//从文件末尾开始逐行读取文件内容
	file.Seek(0, 2)
	reader := bufio.NewReader(file) //包装文件

	for {
		line, _, err := reader.ReadLine()

		if err == io.EOF { //读取到了文件的末尾
			continue
		} else if err != nil {
			panic(fmt.Sprintf("ReadBytes error:%s", err.Error()))
		}
		monitor.TypeMonitorChan <- TypeHandleLine
		rc <- line[:len(line)-1] //目的是去掉换行符
	}

}
